// pages/bankcard/bankcard.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card_id:0,
    card_mame:"",
    cards:[],
    showSelect:0,
    banks:[],
    amount:0,
    current_amount:0,
    process_amount:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu({
      complete: (res) => {},
    });
    this.getData();
  },
  getData:function(){
    var context = this;
    wx.request({
      url: app.config.server + '/api/userinfo',
      header:{
        sessionkey: wx.getStorageSync('session_key')
      },
      success: res => {
        context.setData({
          amount: res.data.amount == undefined ? 0 : res.data.amount
        })
      }
    });
    
    wx.request({
      url: app.config.server + '/api/withdraws/process?status=10',
      header:{
        sessionkey: wx.getStorageSync('session_key')
      },
      success: res => {
        console.log(res);
        context.setData({
          process_amount: res.data
        })
      }
    })
    wx.request({
      url: app.config.server + '/api/cards',
      header:{
        sessionkey:wx.getStorageSync('session_key')
      },
      success: res => {
        context.setData({
          card_id:res.data.length > 0? res.data[0].id : '',
          card_name:res.data.length > 0? res.data[0].bank_name + '(' + res.data[0].card_number.substring( res.data[0].card_number.length - 5, res.data[0].card_number.length - 1) + ')': '',
          cards:res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  redirect:function(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  tapselect: function(e){
    this.setData({
      showSelect:1
    })
  },
  tapcard:function(e){
    this.setData({
      card_id:e.currentTarget.dataset.id,
      card_name:e.currentTarget.dataset.name,
      showSelect:0
    })
  },

  tapinput : function(e){
    console.log(e,e.detail.value,this.data.amount);
    if(e.detail.value > this.data.amount ){
      wx.showToast({
        title: '账户余额不足',
      })
      this.setData({
        current_amount:this.data.amount
      })
    }
  },

  tapwithdraw:function(){
    if(this.data.amount > 0){
      wx.request({
        url: app.config.server + '/api/withdraws/create',
        method:"POST",
        header:{
          sessionkey:wx.getStorageSync('session_key')
        },
        data:{
          card_id:this.data.card_id,
          amount:this.data.current_amount,
        },
        success:function(res){
          console.log(res);
          wx.redirectTo({
            url: '/pages/bankcard/bankcard',
          })
        }
      })
    }
    
  }
})